package manage.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import manage.model.*;
import manage.service.RoleServcie;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import manage.base.BaseController;
import manage.service.DepartmentsService;
import manage.service.UserService;
import manage.util.BiSheUtil;
import manage.util.PageInfo;
import manage.util.PageResult;

@Controller
public class UserController extends BaseController<User, UserService<User>> {

	private static Logger logger = LogManager.getLogger(UserController.class);
	@Resource
	private UserService<User> userServiceImpl;
	@Resource
	private DepartmentsService<Departments> departmentsServiceImpl;

	@Autowired
	private RoleServcie roleServcie;

	@RequestMapping("/add")
	public String Register() {
		return "index";
	}



	@RequestMapping(value = "/register", produces = "text/html;charset=UTF-8", method = RequestMethod.DELETE)
	@ResponseBody
	public String delete(@RequestBody String account) {
		System.out.println(account);
		String userAccount = account;
		userServiceImpl.deleteByPrimaryKey(userAccount);
		// String returnInfo = JSONObject.toJSONString("删除成功");
		return "删除成功";

	}

	@RequestMapping(value = "/register", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String add(User user) throws Exception {
		String res = userServiceImpl.validate(user, INSERT);
		// String res = userServiceImpl.insertAdd(user);
		String returnInfo = JSONObject.toJSONString(res);
		return returnInfo;
	}

	@RequestMapping(value = "/register", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	// 分页查询
	public String find(HttpServletRequest request,HttpSession session) {
		 String pageSize = request.getParameter("pageSize");
		 String currentPage = request.getParameter("currentPage");
		 String depNumber = request.getParameter("depNum");
		 String userNum = request.getParameter("userNum");
		 String userName = request.getParameter("userName");
		 if (!BiSheUtil.notNull(depNumber)) {
				depNumber = null;
			}
		 if (!BiSheUtil.notNull(userNum)) {
			 userNum = null;
			}
		 if (!BiSheUtil.notNull(userName)) {
			 userName = null;
			}
		  PageInfo page= getPage(pageSize, currentPage); 
		  @SuppressWarnings("unchecked")
		  List<User> userList = (List<User>) userServiceImpl.selectAll(page,depNumber,userNum,userName);
		List<Role>roles =roleServcie.getALl();
		List<UserInfoExten>userInfoExtens = new ArrayList<UserInfoExten>();
		for(User u:userList){
			UserInfoExten userInfoExten = new UserInfoExten();
			for(Role r:roles){
				if(r.getRoleid()==u.getAuthority()){
					userInfoExten.setAuthorityname(r.getRolename());
					break;
				}
			}
			userInfoExten.setAuthority(u.getAuthority());
			userInfoExten.setAccount(u.getAccount());
			userInfoExten.setUsername(u.getUsername());
			userInfoExten.setPassword(u.getPassword());
			userInfoExten.setDepNumber(u.getDepNumber());
			userInfoExten.setDepName(departmentsServiceImpl.selectByPrimaryKey(u.getDepNumber()).getName());
			userInfoExten.setDlist(u.getDlist());
			userInfoExtens.add(userInfoExten);
		}
		session.setAttribute("roles",roles);
		  validate(page);

		HashMap result = new HashMap();
		result.put("arrayList", userInfoExtens);
		result.put("pageInfo", page);
		String returnInfo = JSONObject.toJSONString(result);
		return returnInfo;
	}


	@RequestMapping(value = "/registerSelect", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String selectByPrimaryKey(@RequestBody String account) {
		String userAccount = account;
		User user = userServiceImpl.selectByPrimaryKey(userAccount);
		String json = JSONObject.toJSONString(user);
		return json;
	}

	@RequestMapping(value = "/register", produces = "text/html;charset=UTF-8", method = RequestMethod.PUT)
	@ResponseBody
	public String update(User user) throws Exception {
		String res = userServiceImpl.validate(user, UPDATE);
		// String res = userServiceImpl.updateByPrimaryKeySelective(user);
		String returnInfo = JSONObject.toJSONString(res);
		return returnInfo;
	}

	/*
	 * @RequestMapping(value="/userSelectTree",
	 * produces="text/html;charset=UTF-8", method=RequestMethod.PUT)
	 * 
	 * @ResponseBody public String selectByDepNumber( User user) throws
	 * Exception { String res = userServiceImpl.validate(user, UPDATE); //
	 * String res = userServiceImpl.updateByPrimaryKeySelective(user); String
	 * returnInfo = JSONObject.toJSONString(res); return returnInfo; }
	 */
	@RequestMapping(value = "/departmentsDel", produces = "text/html;charset=UTF-8", method = RequestMethod.DELETE)
	@ResponseBody
	// 分页查询
	public String deleteList(@RequestBody String deleteList) {
		String[] delete = deleteList.split(",");
		userServiceImpl.deleteList(delete);
		return "删除成功";
	}

}
